package cn.ht.jywrwater.module.ht.service.waterstation;

import cn.ht.jywrwater.module.ht.dal.dataobject.waterpipenet.WaterPipenetDO;
import cn.ht.jywrwater.module.ht.dal.mysql.waterpipenet.WaterPipenetMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.ht.jywrwater.module.ht.controller.admin.waterstation.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterstation.WaterStationDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.waterstation.WaterStationConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.waterstation.WaterStationMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 供水站 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterStationServiceImpl implements WaterStationService {

    @Resource
    private WaterStationMapper waterStationMapper;

    @Resource
    private WaterPipenetMapper waterPipenetMapper;

    @Override
    public Long createWaterStation(WaterStationCreateReqVO createReqVO) {
        // 插入
        WaterStationDO waterStation = WaterStationConvert.INSTANCE.convert(createReqVO);
        waterStationMapper.insert(waterStation);
        // 返回
        return waterStation.getId();
    }

    @Override
    public void updateWaterStation(WaterStationUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterStationExists(updateReqVO.getId());
        // 更新
        WaterStationDO updateObj = WaterStationConvert.INSTANCE.convert(updateReqVO);
        waterStationMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterStation(Long id) {
        // 校验存在
        validateWaterStationExists(id);
        // 删除
        waterStationMapper.deleteById(id);
    }

    private void validateWaterStationExists(Long id) {
        if (waterStationMapper.selectById(id) == null) {
            throw exception(WATER_STATION_NOT_EXISTS);
        }
    }

    @Override
    public WaterStationDO getWaterStation(Long id) {
        return waterStationMapper.selectById(id);
    }

    @Override
    public List<WaterStationGeoRespVO> getWaterStationGeo() {
        List<WaterStationDO> waterStationDOS = waterStationMapper.selectList();
        List<WaterStationGeoRespVO> result = CollUtil.newArrayList();
        for (WaterStationDO waterStationDO : waterStationDOS) {
            WaterStationGeoRespVO waterStationGeoRespVO = WaterStationConvert.INSTANCE.convertGeo(waterStationDO);
            WaterPipenetDO waterPipenetDO = waterPipenetMapper.selectOne("station_name", waterStationDO.getStationCode());
            waterStationGeoRespVO.setGeoJson(waterPipenetDO.getPipenetPosition());
            result.add(waterStationGeoRespVO);
        }
        return result;
    }

    @Override
    public List<WaterStationDO> getWaterStationList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterStationMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterStationDO> getWaterStationPage(WaterStationPageReqVO pageReqVO) {
        return waterStationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterStationDO> getWaterStationList(WaterStationExportReqVO exportReqVO) {
        return waterStationMapper.selectList(exportReqVO);
    }

    @Override
    public List<WaterStationDO> getAllWaterStation() {
        List<WaterStationDO> waterStationList = waterStationMapper.selectList();
        return waterStationList;
    }

}
